//引入代码
var call = require('../../util/request.js');


Page({
	data: {
		num:1,     //初始数量
		condition: "1",
		pafe: '1',
		xian: true,
		yin: false,
		pays: true,
		
		datap:true,
		datas:false,
		
		timeData:'',
		
		userName:'',
		userPwd:''
		// timeData:[
		// 	{
		// 		date:"2020-10-30",
		// 		money:200,
		// 		sen:"avtiv"
				
		// 	},
		// 	{
		// 		date:"2020-10-31",
		// 		money:195,
		// 		sen:"avtiv"
		// 	},
		// 	{
		// 		date:"2020-11-01",
		// 		money:195,
		// 		sen:"avtiv"
		// 	},
		// 	{
		// 		date:"2020-11-02",
		// 		money:195,
		// 		sen:"avtiv"
		// 	},
		// 	{
		// 		date:"2020-11-02",
		// 		money:195,
		// 		sen:"avtiv"
		// 	},
			
		// ]
		
		
	},
	onShow: function() {
		
	},
	onReady: function() {
	
	
	},
	onLoad: function(options) {
		var _this = this;
		
		this.setData({
			name: JSON.parse(options.name)
		})
		
		//门票
		call.getData('api/getPriceCalendar?id=' + options.id, this.getPriceCalendar, this.fail);
		
		
		//获取当前日期
		var timestamp = Date.parse(new Date());
		var date = new Date(timestamp);
		//获取年份  
		var Y = date.getFullYear();
		//获取月份  
		var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
		//获取当日日期 
		var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
		
		
		var that = this;
		that.setData({      //全部时间
			messtimes: + Y  + '-' + M + '-' + D
		})
		that.setData({
			messtime: +M + '-' + D,
			id:options.id
		})
		
	},
	//点击第三个
	paymenttimes: function(item) {
		var that = this;
		
		that.setData({
			pafe: 2,
			total:item.currentTarget.dataset.price/100*that.data.num,
			calendarMoney:item.currentTarget.dataset.price/100,
			timesp:item.currentTarget.dataset.time
		})
		
	},
	paymenttimew: function(item) {
		
		var that = this;
		that.setData({
			pafe: 3,
			total:item.currentTarget.dataset.price*that.data.num,
			calendarMoney:item.currentTarget.dataset.price,
			timesp:item.currentTarget.dataset.time
		})
		
	},
	paymenttime: function(item) {
		if(item.currentTarget.dataset.index!==0){
			
			
			var that = this;
			
			that.setData({
				pafe: 1,
				total:item.currentTarget.dataset.price/100*that.data.num,
				calendarMoney:item.currentTarget.dataset.price/100,
				timesp:item.currentTarget.dataset.time
			})
		}
		
		
	},
	
	getPriceCalendar: function(data) {
		var recieverArr = []; //全局变量
		var timeDataArr = [];
		var that = this;
		//正常
		data.data.forEach((item) => {
			var recieverObj = {
				dates : item.price_date,
				price_date: item.price_date.substring(5, 10),
				price: item.price
			}
			
			var timeDataObj = {
				date : item.price_date,
				money: item.price/100,
				sen:"avtiv"
			}
			
			
			recieverArr.push(recieverObj);
			
			//console.log('征信地址'+JSON.stringify(recieverArr))
			that.setData({
				getPriceCalendartime: recieverArr
			})
		})
		
		if(this.data.messtimes==data.data[0].price_date){
			var sre = data.data.splice(0,1)
			
		}else{
		
		}
		data.data.forEach((item) => {
			var recieverObj = {
				price_date: item.price_date.substring(5, 10),
				price: item.price
			}
			var timeDataObj = {
				date : item.price_date,
				money: item.price/100,
				sen:"avtiv"
			}
			//日历	
			timeDataArr.push(timeDataObj)
			
			that.setData({
				timeData: timeDataArr,
				calendarMoney:recieverArr[1].price/100,
				total:recieverArr[1].price/100*that.data.num,
				timesp:recieverArr[1].dates
			})
			
			
		})
		
		// that.setData({
		// 	getPriceCalendartime: data.data
		// })
	},
	//获取用户输入的用户名
	userNameInput:function(e){
			this.setData({
			userName: e.detail.value
			})
	},
	passWdInput:function(e){
		this.setData({
		userPwd: e.detail.value
		})
	},
	payment: function() {
		var phoneReg = /(^1[3|4|5|7|8|9]\d{9}$)|(^09\d{8}$)/;
		if(this.data.userName==""){
			tt.showToast({
			icon: 'none',
			title: '姓名不能为空',
			});
			return
		}
		if(this.data.userPwd==""){
			tt.showToast({
				icon: 'none',
				title: '手机号码不能为空',
			});
			return
		}
		
		if (!phoneReg.test(this.data.userPwd)) {
		       tt.showToast({
		       	icon: 'none',
		       	title: '请输入正确手机号码'
		       })
		       return
		
		   }
		tt.request({
		  url: "https://dy.nbhysj.com/api/addOrder",
		  data: {
			  username:this.data.userName,
			  mobile:this.data.userPwd,
			  goods_time:this.data.timesp,
			  order_num:this.data.num,
			  goods_id:this.data.id,
			  openid:tt.getStorageSync("key")
		  },
		  method: 'POST',
		  success(data) {
			console.log('进来了吗'+JSON.stringify(data.data.data))
			tt.pay({
			  orderInfo: data.data.data,
			  service: 1,
			  _debug:1,
			  getOrderStatus(res) {
				
			      let { out_order_no } = res;
			      return new Promise(function (resolve, reject) {
				
					
					tt.request({
					  url: "https://dy.nbhysj.com/api/getOrderStatus",
					  data: {out_order_no:out_order_no},
					  method: 'POST',
					  success(res) {
						 // 商户后端查询的微信支付状态，通知收银台支付结果
						  console.log('经来么'+res.data.data.order_status)
						 if(res.data.data.order_status==2){
						
							 resolve({code:0});
							 // tt.navigateTo({
							 // 	url: '/pages/verification/verification?order_no='+res.data.data.id
							 // })
							 // return
						 }
					  },
					  fail(err) {
						reject(err);
					  }
					});  
					
					/* tt.switchTab({ url: '/pages/personal/personal' }) */
				
			
					
			      });
			    },
			  success(res) {
				  console.log('支付状态查询哈哈'+res.code)
				if(res.code==0){
					var postData = {
							out_order_no:data.data.out_order_no
						}
						tt.request({
						  url: "https://dy.nbhysj.com/api/getOrderStatus",
						  data: postData,
						  method: 'POST',
						  success(res) {
							
							 // 商户后端查询的微信支付状态，通知收银台支付结果
							 if(res.data.data.order_status==2){
								 tt.navigateTo({
								 	url: '/pages/verification/verification?order_no='+res.data.data.id
								 })
							 }
						  },
						  fail(err) {
							reject(err);
						  }
						});
				}  
				
					
				
			
			  },
			   fail(res) {
			    
			    },
			});
		  },
		  fail(err) {
			reject(err);
		  }
		});  
		
		/* var postData={
			username:this.data.userName,
			mobile:this.data.userPwd,
			goods_time:this.data.timesp,
			order_num:this.data.num,
			goods_id:this.data.id,
			openid:tt.getStorageSync("key")
		}
		console.log('参数妈妈'+postData)
		call.request('api/addOrder',postData,this.addOrder, this.fail);
		 */
	},
	/* addOrder:function(data){
		
		// console.log('日了'+data.data)
		// if(data.code==10000){
		// 	 tt.navigateTo({
		// 		url: '/pages/pay/pay?orderInfos='+JSON.stringify(data.data) + '&pice='+ JSON.stringify(data.data.total_amount) + '&time='+JSON.stringify(data.data.valid_time),
		// 	})
		// }else{
		// 	tt.showToast({
		// 		icon: 'none',
		// 		title: data.msg
		// 	})
		// }
		
		tt.pay({
		  orderInfo: data.data,
		  service: 1,
		  _debug:1,
		  getOrderStatus(res) {
			
		      let { out_order_no } = res;
		      return new Promise(function (resolve, reject) {
			
				
				tt.request({
				  url: "https://dy.nbhysj.com/api/getOrderStatus",
				  data: {out_order_no:out_order_no},
				  method: 'POST',
				  success(res) {
					 // 商户后端查询的微信支付状态，通知收银台支付结果
					  console.log('经来么'+res.data.data.order_status)
					 if(res.data.data.order_status==2){
					
						 resolve({code:0});
						 // tt.navigateTo({
						 // 	url: '/pages/verification/verification?order_no='+res.data.data.id
						 // })
						 // return
					 }
				  },
				  fail(err) {
					reject(err);
				  }
				});  
				
				/* tt.switchTab({ url: '/pages/personal/personal' }) */
			
		/* 	
				
		      });
		    },
		  success(res) {
			  console.log('支付状态查询哈哈'+res.code)
			if(res.code==0){
				var postData = {
						out_order_no:data.data.out_order_no
					}
					tt.request({
					  url: "https://dy.nbhysj.com/api/getOrderStatus",
					  data: postData,
					  method: 'POST',
					  success(res) {
						
						 // 商户后端查询的微信支付状态，通知收银台支付结果
						 if(res.data.data.order_status==2){
							 tt.navigateTo({
							 	url: '/pages/verification/verification?order_no='+res.data.data.id
							 })
						 }
					  },
					  fail(err) {
						reject(err);
					  }
					});
			}  
			
				
			
		
		  },
		   fail(res) {
		    
		    },
		});
	}, */
	
	//点击我显示底部弹出框
	clickme: function() {
		this.showModal();
	},
	//显示对话框
	showModal: function() {
		this.setData({ //通过setData来修改
			xian: false,
			yin: true
		})
		const yin = true
		// 显示遮罩层
		var animation = wx.createAnimation({
			duration: 200,
			timingFunction: "linear",
			delay: 0
		})
		this.animation = animation
		animation.translateY(300).step()
		this.setData({
			animationData: animation.export(),
			showModalStatus: true
		})
		setTimeout(function() {
			animation.translateY(0).step()
			this.setData({
				animationData: animation.export()
			})
		}.bind(this), 200)
	},
	//隐藏对话框
	hideModal: function() {
		this.setData({ //通过setData来修改
			xian: true,
			yin: false
		})
		// 隐藏遮罩层
		var animation = wx.createAnimation({
			duration: 200,
			timingFunction: "linear",
			delay: 0
		})
		this.animation = animation
		animation.translateY(300).step()
		this.setData({
			animationData: animation.export(),
		})
		setTimeout(function() {
			animation.translateY(0).step()
			this.setData({
				animationData: animation.export(),
				showModalStatus: false
			})
		}.bind(this), 200)
	},
	
	
	
	//显示价格日历控件
	//显示对话框
	showModals: function() {
		// 获取日历组件
		this.dateSelect = this.selectComponent('#dateSelect')
		this.dateSelect.getMonth()
		
		// 显示遮罩层
		var animation = wx.createAnimation({
			duration: 200,
			timingFunction: "linear",
			delay: 0
		})
		this.animation = animation
		animation.translateY(300).step()
		this.setData({
			animationDataS: animation.export(),
			showModalStatuss: true,
			pays:false
		})
		setTimeout(function() {
			animation.translateY(0).step()
			this.setData({
				animationDataS: animation.export()
			})
		}.bind(this), 200)
		
	},
	
	//隐藏价格日历对话框
	hideModals: function() {
		
		// 隐藏遮罩层
		var animation = wx.createAnimation({
			duration: 200,
			timingFunction: "linear",
			delay: 0
		})
		this.animation = animation
		animation.translateY(300).step()
		this.setData({
			animationDataS: animation.export(),
		})
		setTimeout(function() {
			animation.translateY(0).step()
			this.setData({
				animationDataS: animation.export(),
				showModalStatuss: false,
				pays:true
			})
		}.bind(this), 200)
	},
	
	// 日期点击
	  _dayClick:function(e){
		  var weekDay = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
		  var myDate = new Date(Date.parse(e.detail.calendarDate));  
		  console.log(weekDay[myDate.getDay()]);    // 星期五
		  
		  
		  if(this.data.timeData[0].date== e.detail.calendarDate){
			
			  this.setData({
			    calendarDate: e.detail.calendarDate,
			    calendarDatejian: e.detail.calendarDate.substring(5, 10),
			    calendarMoney: e.detail.calendarMoney,
			    calendarMonth: e.detail.calendarMonth,
			    calendarDay: e.detail.calendarDay,
			    calendarweek:weekDay[myDate.getDay()],
			    pafe: 1
			  })
			  
		  }else{
			this.setData({
			  calendarDate: e.detail.calendarDate,
			  calendarDatejian: e.detail.calendarDate.substring(5, 10),
			  calendarMoney: e.detail.calendarMoney,
			  calendarMonth: e.detail.calendarMonth,
			  calendarDay: e.detail.calendarDay,
			  calendarweek:weekDay[myDate.getDay()],
			  pafe: 3
			})
		  }
		
		if(this.data.timesp == e.detail.calendarDate){
		
			this.setData({
				datap:true,
				datas:false,
				pays:true,
				showModalStatuss: false,
				timesp:this.data.calendarDate
				
			})
		}else{
		
			this.setData({
				datap:false,
				datas:true,
				pays:true,
				showModalStatuss: false,
				timesp:this.data.calendarDate
			})
		}
	
		
		  
	  },
	  
	  //加数量
	  bindPlus:function(item){
		  var num =  item.currentTarget.dataset.num;
		  num++;
		  
		  this.setData({
		        num:num,
				total:this.data.calendarMoney*num
		   })
		 //  console.log('金额'+this.data.calendarMoney*this.data.num)
	  },
	  
	//减数量
	bindMinus:function(item){
		var num = item.currentTarget.dataset.num;
		if(num==1){
			tt.showToast({
			  title: '亲,不能再减了',
			});
		}else{
			num--;
			
			this.setData({
				  num:num,
				  total:this.data.calendarMoney*num
			 })
		}
		
	},
	
	purchase:function(e){
		//进入购买须知
		tt.navigateTo({
			url: '/pages/information/information?name='+JSON.stringify(e.currentTarget.dataset.names)
		})
	},

	postOrder:function(data){
		console.log('支付成功毁掉'+data)
	},
	onHide: function() {

	},
	onUnload: function() {

	},
})
